/**   
 *    

 * 项目名称：SmartKungFuService   
 * 类名称：OrderEditServlet   
 * 类描述：   编辑订单接口
 * 创建人：zhangl  
 * 创建时间：2016年9月28日 上午9:57:33   
 * 修改人：zhangl   
 * 修改时间：2016年9月28日 上午9:57:33   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.OrderReqPara;
import com.lst.entity.response.OrderResPara;
import com.lst.model.ClientGrowth;
import com.lst.model.MstOrder;
import com.lst.model.OrderDetail;
import com.lst.service.MstOrderService;
import com.lst.service.OrderDetailService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: OrderEditServlet
 * @Description: 编辑订单接口
 * @author zhangl
 * @date 2016年9月28日 上午9:57:33
 * 
 */
public class OrderEditServlet extends BaseServlet {

    /**
     * @Fields serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    private MstOrderService mstOrderService = ctx
	    .getBean(MstOrderService.class);

    private OrderDetailService orderDetailService = ctx
	    .getBean(OrderDetailService.class);

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	Date startDate = new Date();
	logger.info("doPost begin: " + startDate);

	OrderResPara resPara = new OrderResPara();

	try {
	    OrderReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
		Integer orderid = Integer.valueOf(reqPara.getOrderid());
		Integer orderstatus = Integer.valueOf(reqPara.getOrderstatus());
		// String paydate = reqPara.getPaydate();

		// 订单
		MstOrder mo = mstOrderService.selectByPrimaryKey(orderid);

		MstOrder mstOrder = new MstOrder();
		mstOrder.setId(mo.getId());
		mstOrder.setPaydate(new Date());
		mstOrder.setUpdatedate(startDate);
		mstOrder.setUpdateuser(mo.getClientid());

		// 订单详情
		Map<String, Object> odMap = new HashMap<String, Object>();

		odMap.put("enabled", true);
		odMap.put("isDeleted", false);
		odMap.put("orderid", mo.getId());

		PageBounds pageBounds = new PageBounds();

		List<OrderDetail> od = orderDetailService.queryByList(odMap,
			pageBounds);
		
		od.get(0).setOrderstatus(orderstatus);
		od.get(0).setUpdatedate(startDate);
		od.get(0).setUpdateuser(mo.getClientid());
		
//		ClientGrowth cg = mstOrderService.updateOrderAndOthers(mstOrder, od.get(0));
		mstOrderService.updateOrderAndOthers(mstOrder, od.get(0));
//		if (cg != null) {
		    MstOrder order = mstOrderService.selectByPrimaryKey(orderid);
		    OrderDetail odetail = orderDetailService.selectByPrimaryKey(od.get(0).getId());

//		    resPara.setCg(cg);
		    resPara.setOrder(order);
		    resPara.setOderdetail(odetail);

//		}
		resPara.setCode(CommCode.M_Y000000);
		resPara.setMessage(CommCode.M_Y000001);

		logger.info("doPost end run(s): "
			+ DateUtil.calLastedTime(startDate));
	    }

	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);
	    logger.info(jsonResult);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

	    logger.info("doPost end run(s): "
		    + DateUtil.calLastedTime(startDate));
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public OrderReqPara getReqPara(HttpServletRequest request) {

	OrderReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new OrderReqPara());

	    reqPara = (OrderReqPara) baseRequest.clone();

	    String orderid = request.getParameter("orderid");
	    String orderstatus = request.getParameter("orderstatus");
	    String paydate = request.getParameter("paydate");

	    logger.info("reqPara orderid: " + orderid);
	    logger.info("reqPara orderstatus: " + orderstatus);
	    logger.info("reqPara paydate: " + paydate);

	    reqPara.setOrderid(orderid);
	    reqPara.setOrderstatus(orderstatus);
	    reqPara.setPaydate(paydate);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}

	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	OrderReqPara orderReqPara = (OrderReqPara) reqPara;
	OrderResPara orderResPara = (OrderResPara) resPara;

	if (StringUtils.isEmpty(orderReqPara.getOrderid())) {
	    orderResPara.setCode(CommCode.M_A000000);
	    orderResPara.setMessage(CommCode.M_E000009);
	} else if (StringUtils.isEmpty(orderReqPara.getOrderstatus())) {
	    orderResPara.setCode(CommCode.M_A000000);
	    orderResPara.setMessage(CommCode.M_E000010);
	} else {
	    /*
	     * if(CommCode.SYSTEM_ORDER_STATUS_WAIT.equals(Integer.valueOf(
	     * orderReqPara.getOrderstatus()))){
	     * if(StringUtils.isEmpty(orderReqPara.getPaydate())){
	     * orderResPara.setCode(CommCode.M_A000000);
	     * orderResPara.setMessage(CommCode.M_E000012); } else { flag =
	     * true; } }else{ flag = true; }
	     */

	    flag = true;
	}

	return flag;
    }

}
